package com.hs.srm.admin.modules.cutpay.controller;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import sunnykid.Sunnykid;
import sunnykid.lang.LangException;
import sunnykid.lang.SunnykidDate;
import sunnykid.text.SunnykidText;
import sunnykid.util.MapUtils;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.hs.srm.admin.modules.cutpay.service.CutpayHeaderService;
import com.hs.srm.admin.modules.invoice.service.InvoiceService;
import com.hs.srm.admin.modules.orders.entity.ScmPoChangeEntity;
import com.hs.srm.admin.modules.orders.entity.ScmPoHeaderEntity;
import com.hs.srm.admin.modules.orders.entity.ScmPoItemEntity;
import com.hs.srm.admin.modules.sys.controller.AbstractController;
import com.hs.srm.base.common.annotation.SysLog;
import com.hs.srm.common.utils.PageUtils;
import com.hs.srm.common.utils.R;

/**
 * 
 * <p>標題：扣款单头信息controller层</p>
 * <p>描述：</p>
 * <p>版權：版權所有 (c) 2019</p>
 * <p>組織：厚声集团</p>
 *
 * @author 鐘曉籟
 * @version V1.0.0
 * @date 2019年4月2日 上午9:49:54
 * @since V1.0.0
 */
@RestController
@RequestMapping("/cutpayHeader")
public class CutpayHeaderController extends AbstractController {

	/**
	 * 类序列ID
	 */
	private static final long serialVersionUID = -2392337998173025326L;
	
	@Autowired
	private CutpayHeaderService cutpayHeaderService;
	
	@Autowired
	private InvoiceService invoiceService;
	
	/**
	 * 查询列表
	 */
	@RequestMapping("/list")
	@ResponseBody
	public R list(@RequestParam Map<String, Object> params) {
		PageUtils pu = this.cutpayHeaderService.selectDetailPage(params);
		//List<ScmPoItemVo> voList = (List<ScmPoItemVo>)pu.getList();
		//voList = EntityUtils.valToTxt(voList);
		return R.ok(pu);
	}
	
	/**
	 * 查询所有可供添加扣款单的全部订单号
	 */
	@RequestMapping("/poNosForAdd")
	@ResponseBody
	public R poNosForAdd() {
		List<String> list = this.invoiceService.poNosForAdd();
		return R.ok(list);
	}
	
	/**
	 * 查询所有可供添加扣款单的其它数据
	 */
	@RequestMapping("/detailDataForAdd")
	@ResponseBody
	public R detailDataForAdd(String poNo) {
		Map<String, Object> resMap = this.cutpayHeaderService.detailDataForAddPart1(poNo);
		List<Map<String, Object>> resList = this.cutpayHeaderService.detailDataForAddPart2(poNo);
		resMap.put("items", resList);
		return new R().put("result", resMap);
	}
	
	/**   
	 * 更改订单的信息
	 * @param data
	 * @return
	 */
	@SysLog("提交扣款单信息")
	@RequestMapping(value = "/saveData", produces = "application/json; utf-8")
	public R saveData(@RequestBody JSONObject data) {
//{"poNo":"4800000007","coType":"kklx1","items":[{"itemNo":"20","cutMoney":"40","complainDate":"2019-05-07","remark":"AAA"}]}
		try {
			this.cutpayHeaderService.saveData(data, this.getUser().getUsername());
		} catch(LangException ex) {
			return R.error("日期数据格式有错误！");
		}
		return R.ok();
	}

}
